package com.example.tourpal;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class PartnerSettingActivity extends Activity {

	public static final int RESULT_BACK = 12;

	private Context context;

	private Switch partnerSwitch;
	private Button partnerUp;
	private ImageView back;

	private TextView partnerTitle;
	private TextView partnerText;
	private EditText partnerRequest;

	private int spotId;
	private String spotName;
	private String followSetContent;
	private boolean isFollow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		context = this;

		setContentView(R.layout.partner_up_viewpage);
		spotId = getIntent().getIntExtra("spotId", 0);
		spotName = getIntent().getStringExtra("spotName");

		isFollow = getIntent().getBooleanExtra("followSet", false);
		if (isFollow) {
			followSetContent = getIntent().getStringExtra("followContent");
		}

		partnerTitle = (TextView) findViewById(R.id.partnerTitle);
		partnerTitle.setText(spotName
				+ getResources().getString(R.string.partner_title));

		partnerSwitch = (Switch) findViewById(R.id.partnerSwitch);
		partnerUp = (Button) findViewById(R.id.partnerUpBt);
		back = (ImageView) findViewById(R.id.back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});

		partnerText = (TextView) findViewById(R.id.partnerText);
		partnerRequest = (EditText) findViewById(R.id.partnerRequest);
		partnerRequest.setText(followSetContent);

		if (isFollow) {
			partnerSwitch.setChecked(true);
			partnerText.setVisibility(View.VISIBLE);
			partnerRequest.setVisibility(View.VISIBLE);
		}
		partnerSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
				if (isChecked) {
					partnerText.setVisibility(View.VISIBLE);
					partnerRequest.setVisibility(View.VISIBLE);
				} else {
					partnerText.setVisibility(View.INVISIBLE);
					partnerRequest.setVisibility(View.INVISIBLE);
				}

			}
		});

		partnerUp.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String followText = partnerRequest.getText().toString();
				if (partnerSwitch.isChecked()) {
					if (followText.trim().equals("")) {
						Toast.makeText(context, "Partner have no request?",
								Toast.LENGTH_SHORT).show();
						return;
					}
				} else {
					followText = "";
				}
				Intent intent = new Intent();
				intent.putExtra("spotId", spotId);
				intent.putExtra("spotSet", followText);
				setResult(RESULT_BACK, intent);
				finish();
			}
		});

	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();

		overridePendingTransition(R.anim.main_enter, R.anim.sub_exit);
	}
}
